#!/usr/bin/php 
<?php
class SoftnixEasyBackup{
	private $arr_sources;
	private $dest;
	private $dest_format = "Y-m-d";
	public $test = false; //please change this true for real run 
	function __construct(){
		$this->runcmd("/etc/init.d/mysqld stop");		
	}

	function add_sources($dir){
		if(is_dir($dir)){
			$this->arr_sources[] = $dir;
		}
	}

	function get_sources(){
		return $this->arr_sources;
	}

	function set_dest($dest){
		if(is_dir($dest)){
			$this->dest = $this->addslash($dest);
			return true;
		}
		throw new Exceptions("$dest is not Directory");
	}

	function get_dest(){
		return $this->dest;
	}

	private function addslash($dir){
	        if(substr($dir,-1) != "/"){
        		$dir = $dir."/";
       		}
		return $dir;
	}



	/*
	* $format = "Y-m-d-H-i-s"; //blahblah
	*
	*/
	function set_dest_format($format){
		$this->dest_format = $format;
	}

	function run(){
		if(!$this->arr_sources || !is_array($this->arr_sources)){
			throw new Exceptions("Not found file to backup");
		}
		if(!$this->dest || !$this->dest_format){
			throw new Exception("Please Set destination and destination format");
		}else{
			$date_format = date($this->dest_format);
			$dest = $this->dest;
		}

		foreach($this->arr_sources as $v){
	        	$source = trim($v);
       			$name = basename($source);
       			$cmd = "tar cvpfz $dest"."$date_format-$name.tgz $source";
        		$this->runcmd($cmd);
		}

		
	}

	function __destruct(){
		$this->runcmd("/etc/init.d/mysqld start");
	}

	function runcmd($cmd){
		if($this->test){
			echo $cmd."\n";
		}else{
			passthru($cmd);
		}
	}
}

?>
